home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / utilities / isfast / libtk / getset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.7 KB  |  263 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "tk.h"
  5. #include "private.h"
  6.  
  7. #if defined(__cplusplus) || defined(c_plusplus)
  8. #define class c_class
  9. #endif
  10.  
  11. /******************************************************************************/
  12.  
  13. int tkGetColorMapSize(void)
  14. {
  15.  
  16.     if (!xDisplay) {
  17.     return 0;
  18.     } else {
  19.     return w.vInfoMain->colormap_size;
  20.     }
  21. }
  22.  
  23. /******************************************************************************/
  24.  
  25. void tkGetMouseLoc(int *x, int *y)
  26. {
  27.     int junk;
  28.  
  29.     *x = 0;
  30.     *y = 0;
  31.     XQueryPointer(xDisplay, w.wMain, (Window *)&junk, (Window *)&junk,
  32.           &junk, &junk, x, y, (unsigned int *)&junk);
  33. }
  34.  
  35. /******************************************************************************/
  36.  
  37. Display *tkGetXDisplay(void)
  38. {
  39.  
  40.     return xDisplay;
  41. }
  42.  
  43. /******************************************************************************/
  44.  
  45. Window tkGetXWindow(void)
  46. {
  47.  
  48.     return w.wMain;
  49. }
  50.  
  51. /******************************************************************************/
  52.  
  53. void tkSetFogRamp(int density, int startIndex)
  54. {
  55.     XColor c[256];
  56.     int rShift, gShift, bShift, intensity, fogValues, colorValues;
  57.     int i, j, k;
  58.  
  59.     switch (w.vInfoMain->class) {
  60.       case DirectColor:
  61.     fogValues = 1 << density;
  62.     colorValues = 1 << startIndex;
  63.     for (i = 0; i < colorValues; i++) {
  64.         for (j = 0; j < fogValues; j++) {
  65.         k = i * fogValues + j;
  66.         intensity = i * fogValues + j * colorValues;
  67.         if (intensity > w.vInfoMain->colormap_size) {
  68.             intensity = w.vInfoMain->colormap_size;
  69.         }
  70.         intensity = (intensity << 8) | intensity;
  71.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  72.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  73.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  74.         c[k].pixel = ((k << rShift) & w.vInfoMain->red_mask) |
  75.                  ((k << gShift) & w.vInfoMain->green_mask) |
  76.                  ((k << bShift) & w.vInfoMain->blue_mask);
  77.         c[k].red = (unsigned short)intensity;
  78.         c[k].green = (unsigned short)intensity;
  79.         c[k].blue = (unsigned short)intensity;
  80.         c[k].flags = DoRed | DoGreen | DoBlue;
  81.         }
  82.     }
  83.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  84.     break;
  85.       case GrayScale:
  86.       case PseudoColor:
  87.     fogValues = 1 << density;
  88.     colorValues = 1 << startIndex;
  89.     for (i = 0; i < colorValues; i++) {
  90.         for (j = 0; j < fogValues; j++) {
  91.         k = i * fogValues + j;
  92.         intensity = i * fogValues + j * colorValues;
  93.         if (intensity > w.vInfoMain->colormap_size) {
  94.             intensity = w.vInfoMain->colormap_size;
  95.         }
  96.         intensity = (intensity << 8) | intensity;
  97.         c[k].pixel = k;
  98.         c[k].red = (unsigned short)intensity;
  99.         c[k].green = (unsigned short)intensity;
  100.         c[k].blue = (unsigned short)intensity;
  101.         c[k].flags = DoRed | DoGreen | DoBlue;
  102.         }
  103.     }
  104.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  105.     break;
  106.     }
  107.  
  108.     XSync(xDisplay, 0);
  109. }
  110.  
  111. /******************************************************************************/
  112.  
  113. void tkSetGreyRamp(void)
  114. {
  115.     XColor c[256];
  116.     float intensity;
  117.     int rShift, gShift, bShift, i;
  118.  
  119.     switch (w.vInfoMain->class) {
  120.       case DirectColor:
  121.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  122.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  123.             65535.0 + 0.5;
  124.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  125.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  126.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  127.         c[i].pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  128.              ((i << gShift) & w.vInfoMain->green_mask) |
  129.              ((i << bShift) & w.vInfoMain->blue_mask);
  130.         c[i].red = (unsigned short)intensity;
  131.         c[i].green = (unsigned short)intensity;
  132.         c[i].blue = (unsigned short)intensity;
  133.         c[i].flags = DoRed | DoGreen | DoBlue;
  134.     }
  135.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  136.     break;
  137.       case GrayScale:
  138.       case PseudoColor:
  139.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  140.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  141.             65535.0 + 0.5;
  142.         c[i].pixel = i;
  143.         c[i].red = (unsigned short)intensity;
  144.         c[i].green = (unsigned short)intensity;
  145.         c[i].blue = (unsigned short)intensity;
  146.         c[i].flags = DoRed | DoGreen | DoBlue;
  147.     }
  148.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  149.     break;
  150.     }
  151.  
  152.     XSync(xDisplay, 0);
  153. }
  154.  
  155. /******************************************************************************/
  156.  
  157. void tkSetOneColor(int index, float r, float g, float b)
  158. {
  159.     XColor c;
  160.     int rShift, gShift, bShift;
  161.  
  162.     switch (w.vInfoMain->class) {
  163.       case DirectColor:
  164.     rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  165.     gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  166.     bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  167.     c.pixel = ((index << rShift) & w.vInfoMain->red_mask) |
  168.           ((index << gShift) & w.vInfoMain->green_mask) |
  169.           ((index << bShift) & w.vInfoMain->blue_mask);
  170.     c.red = (unsigned short)(r * 65535.0 + 0.5);
  171.     c.green = (unsigned short)(g * 65535.0 + 0.5);
  172.     c.blue = (unsigned short)(b * 65535.0 + 0.5);
  173.     c.flags = DoRed | DoGreen | DoBlue;
  174.     XStoreColor(xDisplay, w.cMapMain, &c);
  175.     break;
  176.       case GrayScale:
  177.       case PseudoColor:
  178.     if (index < w.vInfoMain->colormap_size) {
  179.         c.pixel = index;
  180.         c.red = (unsigned short)(r * 65535.0 + 0.5);
  181.         c.green = (unsigned short)(g * 65535.0 + 0.5);
  182.         c.blue = (unsigned short)(b * 65535.0 + 0.5);
  183.         c.flags = DoRed | DoGreen | DoBlue;
  184.         XStoreColor(xDisplay, w.cMapMain, &c);
  185.     }
  186.     break;
  187.     }
  188.  
  189.     XSync(xDisplay, 0);
  190. }
  191.  
  192. /******************************************************************************/
  193.  
  194. void tkSetOverlayMap(int size, float *rgb)
  195. {
  196.     XColor c;
  197.     unsigned long *buf;
  198.     int max, i;
  199.  
  200.     if (w.vInfoOverlay->class == PseudoColor) {
  201.     max = (size > w.vInfoOverlay->colormap_size) ?
  202.           w.vInfoOverlay->colormap_size : size;
  203.     buf = (unsigned long *)calloc(max, sizeof(unsigned long));
  204.     XAllocColorCells(xDisplay, w.cMapOverlay, True, NULL, 0, buf, max-1);
  205.     for (i = 1; i < max; i++) {
  206.         c.pixel = i;
  207.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  208.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  209.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  210.         c.flags = DoRed | DoGreen | DoBlue;
  211.         XStoreColor(xDisplay, w.cMapOverlay, &c);
  212.     }
  213.     free(buf);
  214.     }
  215.  
  216.     XSync(xDisplay, 0);
  217. }
  218.  
  219. /******************************************************************************/
  220.  
  221. void tkSetRGBMap(int size, float *rgb)
  222. {
  223.     XColor c;
  224.     int rShift, gShift, bShift, max, i;
  225.  
  226.     switch (w.vInfoMain->class) {
  227.       case DirectColor:
  228.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  229.                           : size;
  230.     for (i = 0; i < max; i++) {
  231.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  232.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  233.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  234.         c.pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  235.               ((i << gShift) & w.vInfoMain->green_mask) |
  236.               ((i << bShift) & w.vInfoMain->blue_mask);
  237.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  238.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  239.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  240.         c.flags = DoRed | DoGreen | DoBlue;
  241.         XStoreColor(xDisplay, w.cMapMain, &c);
  242.     }
  243.     break;
  244.       case GrayScale:
  245.       case PseudoColor:
  246.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  247.                           : size;
  248.     for (i = 0; i < max; i++) {
  249.         c.pixel = i;
  250.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  251.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  252.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  253.         c.flags = DoRed | DoGreen | DoBlue;
  254.         XStoreColor(xDisplay, w.cMapMain, &c);
  255.     }
  256.     break;
  257.     }
  258.  
  259.     XSync(xDisplay, 0);
  260. }
  261.  
  262. /******************************************************************************/
  263.